package com.xingkeduo.business.user.domain;

import com.xingkeduo.business.user.service.ConsumerPrivateService;
import com.xingkeduo.business.user.service.ConsumerService;
import com.xingkeduo.business.user.service.ConsumerStatisticsService;
import com.xingkeduo.pojo.dto.WxMaUserInfo;
import com.xingkeduo.pojo.enums.UserTypeEnums;
import com.xingkeduo.pojo.query.ConsumerPrivateQO;
import com.xingkeduo.pojo.query.ConsumerQO;
import com.xingkeduo.pojo.query.ConsumerStatisticsQO;
import com.xingkeduo.pojo.table.ConsumerPrivateTO;
import com.xingkeduo.pojo.table.ConsumerTO;
import com.xingkeduo.pojo.utils.Base64Utils;
import com.xingkeduo.pojo.utils.DateUtils;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author:gaozifeng
 * @Date 2019/6/5 16:07
 **/
@Slf4j
@Component
public class PushDomain {

    @Autowired
    private ConsumerService consumerService;
    @Autowired
    private ConsumerStatisticsService consumerStatisticsService;
    @Autowired
    private FeignDomain feignDomain;
    @Autowired
    private ConsumerPrivateService consumerPrivateService;

    /**
     * 定时扫描
     *
     * @param :
     * @return : void
     * @author : gaozifeng
     * @date : 2019/6/5 16:08
     */
    public void scanConsumerCutHair() throws Throwable {
        Date date = DateUtils.addSomeDays(new Date(), -30);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(date);
        ConsumerStatisticsQO consumerStatisticsQO = new ConsumerStatisticsQO();
        consumerStatisticsQO.setLastTime(format).setIsDelete(YesNoEnum.NO);
        List<Long> userList = consumerStatisticsService.selectUserListId(consumerStatisticsQO);
        log.info("用户长度:[{}]", userList.size());
//        List<WxMaUserInfo> lis = new LinkedList<>();
//        WxMaUserInfo wxMaUserInfo = new WxMaUserInfo();
//        wxMaUserInfo.setAliUserId("2088712085011173");
//        lis.add(wxMaUserInfo);
//        feignDomain.consumerPushHair(lis);
        if (null == userList || userList.size() <= 0) {
        } else {
            log.info("用户[{}]进行推送剪发提醒,");
            List<WxMaUserInfo> pushList = selectUserInfo(userList);
            feignDomain.consumerPushHair(pushList);
        }
    }

    private List<WxMaUserInfo> selectUserInfo(List<Long> userIds) throws Throwable {
        List<ConsumerTO> consumerList = consumerService.selectConsumerList(new ConsumerQO().setIds(userIds).setIsDelete(YesNoEnum.NO));
        if (null == consumerList || consumerList.size() == 0) {
            throw new BusinessException(BusinessException.Type.DEFAULT, "消费者不存在");
        }
        List<ConsumerPrivateTO> privateList = consumerPrivateService.selectUserPrivateList(new ConsumerPrivateQO().setIds(userIds).setIsDelete(YesNoEnum.NO));
        if (null == privateList || privateList.size() == 0) {
            throw new BusinessException(BusinessException.Type.DEFAULT, "消费者信息不存在");
        }
        if (consumerList.size() != privateList.size()) {
            log.info("user 推送剪发消息数据不一致");
            throw new BusinessException(BusinessException.Type.DEFAULT, "消费者信息错误");
        }
        Map<Long, ConsumerPrivateTO> privateMap = privateList.stream().collect(Collectors.toMap(ConsumerPrivateTO::getId, a -> a, (k1, k2) -> k1));
        List<WxMaUserInfo> returnList = new LinkedList<>();
        for (ConsumerTO co : consumerList) {
            WxMaUserInfo wxMaUserInfo = new WxMaUserInfo();
            ConsumerPrivateTO privateTO = privateMap.get(co.getId());
            if (null != privateTO) {
                if (null != privateTO.getMinaOpenId()) {
                    wxMaUserInfo.setMinaOpenId(privateTO.getMinaOpenId());
                }
                if (null != privateTO.getGzhOpenId()) {
                    wxMaUserInfo.setGzhOpenId(privateTO.getGzhOpenId());
                }
            }
            if (co.getType() == UserTypeEnums.ALI.getType()) {
                wxMaUserInfo.setAliUserId(co.getLogin());
            }
            wxMaUserInfo.setId(String.valueOf(co.getId()));
            wxMaUserInfo.setNickName(co.getName() != null ? Base64Utils.decoderParam(co.getName()) : "");
            returnList.add(wxMaUserInfo);
        }
        return returnList;
    }
}
